﻿using LDAP.Entity;
using System;
using System.Web;
using System.Web.UI;
using System.Linq;

namespace LDAP.Web
{
    /// <summary>
    /// 页面基类
    /// </summary>
    public class BasePage : Page
    {
        //private Settings _settings;

        /// <summary>
        /// 可能为null，使用的时候要记得判断
        /// </summary>
        public User CurrentUser { get; private set; }

        public bool IsLogin { get; private set; }

        /// <summary>
        /// 公用Redirect方法 add by Simple Yeung 2010-11-1
        /// </summary>
        /// <param name="url"></param>
        public void Redirect(string url)
        {
            Response.Redirect(url, true);
        }

        protected override void OnInit(EventArgs e)
        {
            //NLog.Log.DefaultLog().Write("OnInit");
            base.OnInit(e);

            CurrentUser = Auth.GetUser();
            IsLogin = CurrentUser != null;

            if (!IsLogin)
            {
                if(!Request.Path.ToString().StartsWith("/Default.aspx", StringComparison.CurrentCultureIgnoreCase))//如果已经是login页面，就不要跳了
                    RedirectToLoginPage();
            }
        }

        public void RedirectToLoginPage()
        {
            string url = GetRawUrl();// Request.Url.ToString();
            string hash = Tools.StringUtil.MD5(url + "2010-07-05");
            Redirect("/Default.aspx?rtn=" + HttpUtility.UrlEncode(url) + "&hash=" + hash);
        }

        public string R
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["version"] ?? "";
            }
        }

        public string SystemName
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["SystemName"] ?? "";
            }
        }

        public string StaticDomain
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["StaticDomain"] ?? "";
            }
        }

        /// <summary>
        /// 获取当前的Action值
        /// </summary>
        protected string Action
        {
            get
            {
                return Tools.Web.Request.GetQueryString("action");
            }
        }

        /// <summary>
        /// 出错跳转到提示页面
        /// </summary>
        /// <param name="message">提示信息</param>
        protected void RedirectToMessagePage(string message)
        {
            Server.Transfer(string.Format("~/exception?message={0}&type=1", HttpUtility.UrlEncode(message)));
        }

        /// <summary>
        /// 跳转到没有权限页面
        /// </summary>
        /// <param name="message">提示信息</param>
        protected void RedirectToAuthorityPage(string message)
        {
            Server.Transfer(string.Format("~/exception.aspx?message={0}&type=2", HttpUtility.UrlEncode(message)));
        }

        /// <summary>
        /// 跳转到404错误页面
        /// </summary>
        /// <param name="message">提示信息</param>
        protected void RedirectTo404Page(string message)
        {
            Server.Transfer(string.Format("~/exception.aspx?message={0}&type=3", HttpUtility.UrlEncode(message)));
        }

        /// <summary>
        /// 得到原始的URL地址，如果经过Intelligencia.UrlRewriter，则返回地址栏里的URL 2010-09-14 增加 by 杜有发
        /// </summary>
        /// <returns></returns>
        public string GetRawUrl()
        {
            HttpContext context = HttpContext.Current;
            if (context == null)
                return null;
            if (context.Items["UrlRewriter.NET.RawUrl"] != null)
            {
                bool isHttp = context.Items["Rewriter.HTTPS"].ToString() == "off";
                bool isDefaultPort = context.Items["Rewriter.SERVER_PORT"].ToString() == "80";
                return string.Format("http{0}://{1}{2}{3}",
                                     isHttp ? "" : "s",
                                     context.Items["Rewriter.HTTP_HOST"],
                                     isDefaultPort ? "" : ":" + context.Items["Rewriter.SERVER_PORT"],
                                     context.Items["UrlRewriter.NET.RawUrl"]
                    );
            }
            return context.Request.Url.ToString();
        }
        #region json转换统一入口 2010-11-18 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(object obj)
        {
            return Utility.JsonTransfer.ToJson(obj);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(string value)
        {
            return Utility.JsonTransfer.DeserializeObject<T>(value);
        }

        #endregion
    }
}